<template>
  <view>
    <!-- -->
    <permissionModel ref="permissionModel" :permissionContent="permissionContent" />
    <model ref="model" :isPush="true" :content="content" :openAppPermission="true" @toOpenAppPermission="toOpenAppPermission" />
  </view>
</template>
<script>
import permision from "@/js_sdk/wa-permission/permission.js"
import {
  openAppSettingPermissions,
  requestAndroidPermission
} from '@/common/index.js'
export default {
  name: 'DongxianUploadImg',
  props: {
    value: {
      type: [Array, String],
      default: ''
    },
    permissionContent: {
      type: String,
      default: '推送新消息，新报价消息，被关注消息需要打开消息推送的权限，请到应用设置里面开启。'
    },
    singleNum: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    imageUrl: {
      get() {
        return this.value || []
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  data() {
    return {
      content: '',
      content1: '获取最新消息，最新报价消息，被关注消息需要打开消息推送的权限，请到应用设置里面开启。',
      show: false,
      columns: [
        [{
          label: '相机',
          id: 1
        }, {
          label: '相册',
          id: 2
        }]
      ],
    };
  },

  mounted() {

  },

  methods: {
    toOpenAppPermission() {
      this.$refs.permissionModel.close()
    },
    async open(isShow) {
      // #ifdef APP-PLUS
      let {
        platform
      } = uni.getSystemInfoSync('systemInfo')
      if (platform == 'android') {
        return this.androidPermission('push', this.content1, this.permissionContent, isShow)
      } else if (platform == 'ios') {
        console.log('%c [ platform ]-80', 'font-size:13px; background:pink; color:#bf2c9f;', platform)
      }
      // #endif
    },
    async androidPermission(type, content, permissionContent, isShow) {
      console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
      let options = {
        'push': 'android.permission.RECEIVE_WAP_PUSH'
      }
      let permission = options[type]
      // let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
      console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
      var Manifest = plus.android.importClass("android.Manifest");
      var MainActivity = plus.android.runtimeMainActivity();
      let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
      console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
      let permissionResult = 0
      if (result) {
        plus.android.requestPermissions([permission], e => {
          console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            if (isShow === 'show') {
              this.content = content
              this.$refs.model.open()
              this.$refs.permissionModel.close()
            }
            permissionResult = 1
          } else if (e.deniedPresent.length > 0) {
            permissionResult = 2
            this.$refs.permissionModel.close()
          } else if (e.granted.length > 0) {
            permissionResult = 3
            this.$refs.permissionModel.close(); // 别忘了关闭提示
          }
        })
        // this.uploadCamera(); // 有权限则直接调用方法
      } else {
        // this.uploadCamera()
        this.permissionContent = permissionContent
        this.$refs.permissionModel.open()
        // this.permissionContent = ''
        plus.android.requestPermissions([permission], e => {
          console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            if (isShow === 'show') {
              this.content = content
              this.$refs.model.open()
              this.$refs.permissionModel.close()
            }
            permissionResult = 1
          } else if (e.deniedPresent.length > 0) {
            this.$refs.permissionModel.close()
            permissionResult = 2
          } else if (e.granted.length > 0) {
            permissionResult = 3
            this.$refs.permissionModel.close(); // 别忘了关闭提示
          }
        })
      }
      return permissionResult
    },
    getQuanxian() {
      let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
      console.log(platform);
      if (platform == "ios") { //这里是ios的方法
        console.log("我是iOS");
        var UIApplication = plus.ios.import("UIApplication");
        var app = UIApplication.sharedApplication();
        var enabledTypes = 0;
        if (app.currentUserNotificationSettings) {
          var settings = app.currentUserNotificationSettings();
          enabledTypes = settings.plusGetAttribute("types");
          console.log("enabledTypes1:" + enabledTypes);
          if (enabledTypes == 0) { //如果enabledTypes = 0 就是通知权限没有开启
            // uni.showModal({
            //   title: '提示',
            //   content: '是否前往打开通知权限',
            //   success: res => {
            //     if (res.confirm) {
            //       this.openTongZhi()
            //     } else if (res.cancel) {
            //       console.log('用户点击取消');
            //     }
            //   }
            // });
            this.content = this.content1
            this.$refs.model.open()
            return false
          } else {
            uni.showToast({
              title: '已开启',
              icon: "none"
            })
            return true
          }
        }
        plus.ios.deleteObject(settings);
      } else if (platform == "android") { //下面是安卓的方法
        console.log("我是安卓", plus.android);
        var main = plus.android.runtimeMainActivity();
        var pkName = main.getPackageName();
        var uid = main.getApplicationInfo().plusGetAttribute("uid");
        var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
        //android.support.v4升级为androidx
        if (NotificationManagerCompat == null) {
            NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
        }
        var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
        // 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
        if (areNotificationsEnabled) {
          uni.showToast({
            title: '已开启',
            icon: "none"
          })
          return true
        } else {
          this.content = this.content1
          this.$refs.model.open()
          return false
        }
      }
    },


  }
};
</script>